const util = require('./util.js')
// post请求方法
// version 是版本号，前两位是大版本号,最后一个数字是小版本号
function postRequest(url, postData) {
  var promise = new Promise((resolve, reject) => {
    const baseUrl = 'https://formal.honghuabin.com/v1/'
    // const baseUrl = 'http://api.yg.com/v1/'
    postData.version = 'V2'
    wx.showToast({
      icon: 'loading',
      title: '加载中'
    })
    wx.request({
      url: baseUrl + url,
      method: 'POST',
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      data: postData,
      success: (res) => {//服务器返回数据
        if (res.data.error == 0) {
          wx.hideToast()
          resolve(res.data)
        } else if (res.data.error == 1) {
          util.showFail(res.data.msg)
          resolve(res.data.msg)
        } else if (res.data.error == 2) {
          resolve(res.data)
        } else {
          reject(res.data.msg)
        }
      },
      error: (rej) => {
        reject('网络出错');
      }
    })
  });
  return promise;
}

module.exports = {
  POST: postRequest
}